package com.zpeb.pdd.controller;

import com.zpeb.pdd.entity.Chat;
import com.zpeb.pdd.mapper.ChatMapper;
import com.zpeb.pdd.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("chatAction")
public class ChatController {
    @Autowired
    ChatMapper chatMapper;

    /**
     * 删除聊天消息
     * @param params
     * @return
     */
    @PostMapping("delete")
    public Msg deleteInformation(@RequestBody List<String> params){
        chatMapper.deleteChat(params);
        return  Msg.success().add("msg","删除成功！");
    }
    /**
     * 获取离线消息
     * @param id
     * @return
     */
    @GetMapping("get/{id}")
    public Msg getOffInformation(@PathVariable String id){
        //获取离线消息
        List<Chat> chats=chatMapper.getOffinformation(id);
        List<String> p=new ArrayList<>();
        for(Chat chat:chats){
            p.add(chat.getChatid());
        }
        chatMapper.updateChatBatch(p);//变成在线消息
        return Msg.success().add("msg",chats);
    }
    /**
     * 获取消息记录
     * @param getInformationRecords
     * @return
     */
    @PostMapping("get")
    public Msg getInformationRecords(@RequestBody Map<String,String> getInformationRecords){
        //获取离线消息
        List<Chat> chats=chatMapper.getInformationRecords(getInformationRecords);
        return Msg.success().add("msg",chats);
    }
}
